package com.hhd.flow.engine.cmd.impl;

import com.hhd.flow.engine.utils.CommonUtils;
import com.hhd.flow.engine.utils.FlowUtils;
import com.hhd.flow.entity.FlowUserTask;
import com.hhd.flow.entity.FlowUserTaskHistory;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.SuperBuilder;

import java.time.LocalDateTime;
import java.util.Date;

@Setter
@Getter
@SuperBuilder(toBuilder = true)
public class CompleteNotifyCommand extends BaseCommand<Void> {
    private String flowUserTaskId;

    @Override
    public Void execute() {
        FlowUserTask flowUserTask = getEngine().getFlowUserTaskService().getById(flowUserTaskId);
        if (CommonUtils.isEmpty(flowUserTask)) return null;

        FlowUtils.endFlowUserTask(flowUserTask, null, null);
        FlowUserTaskHistory flowUserTaskHistory = CommonUtils.copyTo(flowUserTask, FlowUserTaskHistory.class);
        flowUserTaskHistory.setCompleteTime(new Date());
        getEngine().getFlowUserTaskHistoryService().save(flowUserTaskHistory);
        getEngine().getFlowUserTaskService().removeById(flowUserTaskId);
        forFlowUserDone(flowUserTask.getFormId());
        return null;
    }
}
